package top.lishuoboy.hibernate.validator.a5;

import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;
import top.lishuoboy.hibernate.validator.a5.validate.group.InsertGroup;
import top.lishuoboy.hibernate.validator.a5.validate.group.UpdateGroup;

import java.util.ArrayList;
import java.util.List;

public class UserGroupSequenceProvider implements DefaultGroupSequenceProvider<User> {

    @Override
    public List<Class<?>> getValidationGroups(User user) {
        List<Class<?>> defaultGroupSequence = new ArrayList<Class<?>>();  // 【注意】List是有序的，根据add组顺序校验

        if (user != null) {
            if (user.getId() != null) {
                defaultGroupSequence.add(UpdateGroup.class);
            } else {
                defaultGroupSequence.add(InsertGroup.class);
            }
        }

        defaultGroupSequence.add(User.class);
        return defaultGroupSequence;
    }
}